#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDTCInfomation_0x19_test.py
@Time    :   2024/09/05 13:31:28
@Author  :   Yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import time
import Set_up
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable
from commDriver.modbus_plc import *

from sqlalchemy import false
sys.dont_write_bytecode = True


DTCStatusMask_list = [([0x19, 0x02, 0x01], [0x59, 0x02, 0x09]),
                      ([0x19, 0x02, 0x08], [0x59, 0x02, 0x09]),
                      ([0x19, 0x02, 0x09], [0x59, 0x02, 0x09]),
                      ([0x19, 0x02, 0x0A], [0x59, 0x02, 0x09]),
                      ([0x19, 0x02, 0x0B], [0x59, 0x02, 0x09]),
                      ([0x19, 0x02, 0xFF], [0x59, 0x02, 0x09])
                      ]


class Test_ReadDTCInfomation_phy(object):

    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in DTCStatusMask_list]
    @pytest.mark.smoke
    @allure.story('Test DTCStatusMask Test')
    @allure.severity('normal')
    @allure.description('Test request DTCStatusMask with no fault in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', DTCStatusMask_list, ids=ids)
    def test_NoFault_DTCStatusMask_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)

    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in DTCStatusMask_list]
    @pytest.mark.smoke
    @allure.story('Test DTCStatusMask Test')
    @allure.severity('normal')
    @allure.description('Test request DTCStatusMask with no fault in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', DTCStatusMask_list, ids=ids)
    def test_NoFault_DTCStatusMask_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14, 0xFF, 0xFF, 0xFF], [0x54], 3, iStep, False)
        time.sleep(2)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)

    @allure.severity('normal')
    @allure.description('Test request DTCStatusMask with fault in default session with physical addressing')
    @allure.story('Test read dtc information service 0x19')
    @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    @pytest.mark.repeat(10)
    def test_reportDTCByStatusMask_No_Fault_Restart_withPowerOff_Phy(self, action, Driver):
        iStep = 1
        time.sleep(5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 1, iStep, False, 'UDS', 'ClearFault')
        Dtc = uds_resp
        iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 1, iStep, False, 'UDS', 'ClearFault')
        if  uds_resp != Dtc:
               logging.error(f'front restart:[{Dtc}], after restart:[{uds_resp}]')
        with assume: assert uds_resp == Dtc


    @allure.severity('normal')
    @allure.story('Test read dtc information service 0x19')
    @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    @pytest.mark.repeat(10)
    def test_reportDTCByStatusMask_No_Fault_Restart_with11_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 1, iStep, False, 'UDS', 'ClearFault')
        Dtc = uds_resp
        iStep = Driver['CAN'].restartMRR_11(Driver,iStep)
        time.sleep(5)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 1, iStep, False, 'UDS', 'noClearFault')
        if  uds_resp != Dtc:
               logging.error(f'front restart:[{Dtc}], after restart:[{uds_resp}]')
        with assume: assert uds_resp == Dtc

    
    @allure.severity('normal')
    @allure.story('Test read dtc information service 0x19')
    @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_Over_voltage_ByStatusMask_defaul_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 1, iStep, False, 'UDS', 'ClearFault')
        # 设置电压为17V，并等待1.5s 制造 Overvoltage 故障，读取当前故障
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09,0x98,0x7A,0x17,0x09], 0, iStep, False, 'UDS', 'noClearFault')
        # 设置电压为12V，并等待1.5s 恢复 Overvoltage 故障，读取当前故障
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 1, iStep, False, 'UDS', 'noClearFault')

    @allure.severity('normal')
    @allure.story('Test read dtc information service 0x19')
    @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
    def test_Under_voltage_ByStatusMask_extended_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 3, iStep, False, 'UDS', 'ClearFault')
        # 设置电压为8V，并等待1.5s 制造 Undervoltage 故障，读取当前故障 
        iStep = Driver['CAN'].SetVolt(Driver, 8, 1.5, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09,0x98,0x7A,0x16,0x09], 0, iStep, False, 'UDS', 'noClearFault')

        # 设置电压为12V，并等待1.5s 恢复 Undervoltage 故障，读取当前故障
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 3, iStep, False, 'UDS', 'noClearFault')
